//
//  PRTMainGameScene.m
//  Pirates
//
//  Created by goofara on 4/4/14.
//  Copyright (c) 2014 Training Camp. All rights reserved.
//

#import "PRTMainGameScene.h"
#import "PRTPlayer.h"
#import "PRTLevel.h"
#import "PRTLevelLoader.h"

@interface PRTMainGameScene ()

@property (nonatomic) PRTPlayer *player;
@property (nonatomic) PRTLevel * level;

@end

@implementation PRTMainGameScene
{
    CGSize _winSize;
    CCPhysicsNode *_physicsWorld;
    CCSprite *_frontPoint;
    CCSprite *_leftPoint;
    CCSprite *_rightPoint;
    CCSprite *_enemyShip;
    CCSprite *_turretSprite;
    int _turretHP;
    int _playerHP;
    int _enemyShipHP;
    CFTimeInterval _enemyShipStrikeTime;
    CFTimeInterval _turretStrikeTime;
    CCDrawNode *_playerHealthBar;
    CCDrawNode *_turretHealthBar;
    CCDrawNode *_enemyShipHealthBar;
    UIButton *_miniMapButton;
    BOOL _isBigMiniMap;
    CGRect _smallFrame;
    CGRect _bigFrame;
    UIView* _panView;
    CGFloat _currentOrientationOfPlayer;

}

const int MAX_HP = 10000000;
const float RADIUS_OF_FRONT_STRIKE = 200;
const int RADIUS_OF_SIDE_STRIKE = 100;
const int ENEMY_SHIP_VISION_RADIUS = 20; //at 250 will see PlayerShip
const int ENEMY_SHIP_DISTANCE_FROM_PLAYER = 50;
const int ENEMY_SHIP_STRIKE_RADIUS = 150;
const float ENEMY_SHIP_STRIKE_INTERVAL = 1.0f;
const int TURRET_VISION_RADIUS = 20; //at 238 will see PlayerShip
const float TURRET_STRIKE_INTERVAL = 1.5f;
const float HealthBarWidth = 40.0f;
const float HealthBarHeight = 4.0f;
const int SHAKE_ACTION_TAG = 55;

+(PRTMainGameScene *)scene
{
    return [[self alloc] init];
}

-(id)init
{
    self = [super init];
    if(self)
    {
        _winSize = [[CCDirector sharedDirector] viewSize];
        // Add PhysicsNode
        _physicsWorld = [CCPhysicsNode node];
        _physicsWorld.gravity = ccp(0,0);
        _physicsWorld.debugDraw = YES;
        _physicsWorld.collisionDelegate = self;
        
        
        CCSprite * playerSprite = [[CCSprite alloc] initWithImageNamed:@"shipHD1.png"];
//        [playerSprite setPositionType:CCPositionTypeNormalized];
//        [playerSprite setPosition:CGPointMake(0.5f, 0.5f)];
        playerSprite.position  = ccp(_winSize.width/2, _winSize.height/2);
        playerSprite.rotation = 180;
        playerSprite.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, playerSprite.contentSize} cornerRadius:0];
        playerSprite.physicsBody.collisionGroup = @"playerGroup";
        playerSprite.physicsBody.collisionType = @"shipCollision";
        _playerHP = MAX_HP;
        
        
        PRTPlayerStats * stats = [[PRTPlayerStats alloc] init];
        self.player = [[PRTPlayer alloc] initWithPosition:CGPointMake(0, 0) rotation:0 andSprite:playerSprite];
        self.player.speed = 0.7;
        self.player.playerStats = stats;
        CGRect screenBounds = [CCDirector sharedDirector].view.bounds;
        self.player.playerOffsetFromOrigin = CGPointMake(screenBounds.size.width *0.5f, screenBounds.size.height *0.5f);
        self.level = [[PRTLevel alloc] initWithTiledMap:@"level1TileMapV2.tmx"];
        self.player.delegate = self.level;
        
        //[self addChild:self.level.tiledMap];
        [self addChild:self.level.tiledMap];
        [self addChild:_physicsWorld];
        [_physicsWorld addChild:playerSprite];
        //[self addChild:playerSprite];

        [self schedule:@selector(movePlayer:) interval:0.01];
        
//        [self setupSteering];
        [self setupSpeedControl];
        //
        [self addButtonsOnScreen];
        [self addSpritesOnScreen];
        _panView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenBounds.size.width/2, screenBounds.size.height)];
        [[[CCDirector sharedDirector]view]addSubview:_panView];
        UIPanGestureRecognizer* panRec = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
        [_panView addGestureRecognizer:panRec];
        
        //[self loadCloudParticleEffect];

    }
    return self;
}

-(void)update:(CCTime)delta
{
    [self turretStrike];
    [self enemyShipMoveToPlayer];
    [self enemyShipStrike];
    [self updateHealthBars];
    
}

-(void) setupSpeedControl
{
    CCButton * increase = [CCButton buttonWithTitle:@"Speed++"];
    increase.positionType = CCPositionTypeNormalized;
    increase.position = CGPointMake(0.9, 0.9);
    [increase setTarget:self selector:@selector(changeSpeed:)];
    CCButton * decrease = [CCButton buttonWithTitle:@"Speed--"];
    decrease.positionType = CCPositionTypeNormalized;
    decrease.position = CGPointMake(0.7, 0.9);
    [decrease setTarget:self selector:@selector(changeSpeed:)];
    [self addChild:increase];
    [self addChild:decrease];
}

//-(void) setupSteering
//{
//    CCButton * steerLeft = [CCButton buttonWithTitle:@"Left"];
//    steerLeft.positionType = CCPositionTypeNormalized;
//    steerLeft.position = CGPointMake(0.1, 0.1);
//    [steerLeft setTarget:self selector:@selector(steerPlayer:)];
//    CCButton * steerRight = [CCButton buttonWithTitle:@"Right"];
//    steerRight.positionType = CCPositionTypeNormalized;
//    steerRight.position = CGPointMake(0.9, 0.1);
//    [steerRight setTarget:self selector:@selector(steerPlayer:)];
//    [self addChild:steerLeft];
//    [self addChild:steerRight];
//}

-(void) movePlayer:(CCTime)deltaTime
{
    self.level.tiledMap.position = [self.player move];
}

-(void) steerPlayer:(CGFloat) angle
{
    [self.player steerWithDirectionalAngle:angle];
}

-(void) changeSpeed:(id) sender
{
    CCButton * speedButton = (CCButton *)sender;
    if([speedButton.title isEqualToString:@"Speed++"])
    {
        [self.player increaseSpeed];
    }
    else
    {
        [self.player decreaseSpeed];
    }
}

//--------------------------------------------------------------------
-(void)addButtonsOnScreen
{
    //Add Fire Buttons
    CCButton *fireButton = [CCButton buttonWithTitle:@"[FIRE]" fontName:@"Verdana-Bold" fontSize:18.0f];
    fireButton.positionType = CCPositionTypeNormalized;
    fireButton.position = ccp(0.85f, 0.2f); // Fire
    [fireButton setTarget:self selector:@selector(frontFire)];
    [self addChild:fireButton];
    
    CCButton *fireSideButton = [CCButton buttonWithTitle:@"[SIDE FIRE]" fontName:@"Verdana-Bold" fontSize:18.0f];
    fireSideButton.positionType = CCPositionTypeNormalized;
    fireSideButton.position = ccp(0.85f, 0.3f); // Fire
    [fireSideButton setTarget:self selector:@selector(sideFire)];
    [self addChild:fireSideButton];
}

-(void)addSpritesOnScreen
{
    _playerHealthBar = [[CCDrawNode alloc] init];
    _playerHealthBar.contentSize = CGSizeMake(HealthBarWidth, HealthBarHeight);
    [self addChild:_playerHealthBar];
    
    _enemyShipHealthBar = [[CCDrawNode alloc] init];
    _enemyShipHealthBar.contentSize = CGSizeMake(HealthBarWidth, HealthBarHeight);
    [self addChild:_enemyShipHealthBar];
    
    _turretHealthBar = [[CCDrawNode alloc] init];
    _turretHealthBar.contentSize = CGSizeMake(HealthBarWidth, HealthBarHeight);
    [self addChild:_turretHealthBar];
    
    
    //add Fire points (Front ,Left ,Right)
    _frontPoint = [CCSprite spriteWithImageNamed:@"TurretMissile.png"];
    _frontPoint.position = CGPointMake(_player.playerImage.contentSize.width/2, -RADIUS_OF_FRONT_STRIKE);
    [_player.playerImage addChild:_frontPoint];
    
    _leftPoint = [CCSprite spriteWithImageNamed:@"TurretMissile.png"];
    _leftPoint.position = CGPointMake(_player.playerImage.contentSize.width/2 - RADIUS_OF_SIDE_STRIKE, _player.playerImage.contentSize.height/2 );
    [_player.playerImage addChild:_leftPoint];
    
    _rightPoint = [CCSprite spriteWithImageNamed:@"TurretMissile.png"];
    _rightPoint.position = CGPointMake(_player.playerImage.contentSize.width/2 + RADIUS_OF_SIDE_STRIKE, _player.playerImage.contentSize.height/2);
    [_player.playerImage addChild:_rightPoint];
    
    // Add Enemy ship
    _enemyShip = [CCSprite spriteWithImageNamed:@"playerSmall.png"];
    _enemyShip.position  = ccp(_winSize.width/2+150, _winSize.height/2+150);
    _enemyShip.rotation = 180;
    _enemyShip.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, _enemyShip.contentSize} cornerRadius:0];
    _enemyShip.physicsBody.collisionGroup = @"enemyGroup";
    _enemyShip.physicsBody.collisionType = @"enemyShip";
    _enemyShipHP = MAX_HP ;
    [_physicsWorld addChild:_enemyShip];
    
    // Add a Turret sprite
    _turretSprite = [CCSprite spriteWithImageNamed:@"Turret.png"];
    _turretSprite.position = CGPointMake(50, 200);
    _turretSprite.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, _turretSprite.contentSize} cornerRadius:0];
    _turretSprite.physicsBody.collisionGroup = @"enemyGroup";
    _turretSprite.physicsBody.collisionType = @"turretCollision";
    _turretHP = MAX_HP;
    [_physicsWorld addChild:_turretSprite];
    
    // Add MiniMap
    _smallFrame = CGRectMake(_winSize.width -80, 50, 90, 80);
    _bigFrame = CGRectMake(0 , 20, _winSize.width, _winSize.height-20);
    _miniMapButton = [[UIButton alloc]initWithFrame:_smallFrame];
    UIImage *btnImage = [UIImage imageNamed:@"MiniMap.png"];
    [_miniMapButton setImage:btnImage forState:UIControlStateNormal];
    [_miniMapButton addTarget:self action:@selector(resizeMiniMap) forControlEvents:UIControlEventTouchUpInside];
    [[[CCDirector sharedDirector] view] addSubview:_miniMapButton];
}

-(void)resizeMiniMap
{
    if (_isBigMiniMap) {
        _isBigMiniMap = NO;
        _panView.hidden = NO;
        [UIView animateWithDuration:1 animations:^{
            _miniMapButton.frame = _smallFrame;
            [[CCDirector sharedDirector] resume];
        }];
        //NSLog(@"To Small Map !");
    } else {
        [[CCDirector sharedDirector] pause];
        _isBigMiniMap = YES;
        _panView.hidden = YES;
        [UIView animateWithDuration:1 animations:^{
            _miniMapButton.frame = _bigFrame;
        }];
        //NSLog(@"To Big Map !");
    }
}


-(void)updateHealthBars
{
    _playerHealthBar.position = ccp(_player.playerImage.position.x - HealthBarWidth/2.0f + 0.5f,
                                    _player.playerImage.position.y - _player.playerImage.contentSize.height/2.0f - 15.0f + 0.5f);
    _enemyShipHealthBar.position = ccp(_enemyShip.position.x - HealthBarWidth/2.0f + 0.5f,
                                      _enemyShip.position.y - _enemyShip.contentSize.height/2.0f - 10.0f + 0.5f);
    _turretHealthBar.position = ccp(_turretSprite.position.x - HealthBarWidth/2.0f + 0.5f,
                                    _turretSprite.position.y - _turretSprite.contentSize.height/2.0f - 10.0f + 0.5f);
    [self drawHealthBar:_playerHealthBar hp:_playerHP];
    [self drawHealthBar:_enemyShipHealthBar hp:_enemyShipHP];
    [self drawHealthBar:_turretHealthBar hp:_turretHP];
}

- (void)drawHealthBar:(CCDrawNode *)node hp:(int)hp
{
    [node clear];
    
    CGPoint verts[4];
    verts[0] = ccp(0.0f, 0.0f);
    verts[1] = ccp(0.0f, HealthBarHeight - 1.0f);
    verts[2] = ccp(HealthBarWidth - 1.0f, HealthBarHeight - 1.0f);
    verts[3] = ccp(HealthBarWidth - 1.0f, 0.0f);
    
    CCColor *clearColor = [CCColor colorWithCcColor4f:ccc4f(0.0f, 0.0f, 0.0f, 0.0f)];
    CCColor *fillColor = [CCColor colorWithCcColor4f:ccc4f(113.0f/255.0f, 202.0f/255.0f, 53.0f/255.0f, 1.0f)];
    CCColor *borderColor = [CCColor colorWithCcColor4f:ccc4f(35.0f/255.0f, 28.0f/255.0f, 40.0f/255.0f, 1.0f)];
    
    [node drawPolyWithVerts:verts count:4 fillColor:clearColor borderWidth:1.0f borderColor:borderColor];
    
    verts[0].x += 0.5f;
    verts[0].y += 0.5f;
    verts[1].x += 0.5f;
    verts[1].y -= 0.5f;
    verts[2].x = (HealthBarWidth - 2.0f)*hp/MAX_HP + 0.5f;
    verts[2].y -= 0.5f;
    verts[3].x = verts[2].x;
    verts[3].y += 0.5f;
    
    [node drawPolyWithVerts:verts count:4 fillColor:fillColor borderWidth:0.0f borderColor:clearColor];
}

-(void)frontFire
{
    [self fireWithCannon:_player.playerImage toSprite:_frontPoint withCollisionType:@"playerMissile" andCollisionGroup:@"playerGroup"];
}

-(void)sideFire
{
    [self fireWithCannon:_player.playerImage toSprite:_leftPoint withCollisionType:@"playerMissile" andCollisionGroup:@"playerGroup"];
    [self fireWithCannon:_player.playerImage toSprite:_rightPoint withCollisionType:@"playerMissile" andCollisionGroup:@"playerGroup"];
}

-(void)fireWithCannon:(CCSprite *)cannon toSprite:(CCSprite *)target withCollisionType:(NSString *)collisionType andCollisionGroup:(NSString *)collisionGroup
{
    CCSprite *missile = [CCSprite spriteWithImageNamed:@"PlayerMissile.png"];
    CGPoint cannonToSceneCoordinates = cannon.positionInPoints;//[cannon  convertToWorldSpace: cannon.position];
//    cannonToSceneCoordinates.x -= cannon.contentSize.width/2;
//    cannonToSceneCoordinates.y -= cannon.contentSize.height/2;
    //NSLog(@"cannon position x: %f y: %f",cannonToSceneCoordinates.x,cannonToSceneCoordinates.y);
    //NSLog(@"playerImage position x: %f y: %f",_player.playerImage.position.x,_player.playerImage.position.y);
    missile.position = cannonToSceneCoordinates;
    missile.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, missile.contentSize} cornerRadius:0];
    missile.physicsBody.collisionGroup = collisionGroup;
    missile.physicsBody.collisionType = collisionType;
    [_physicsWorld addChild:missile];
    
    // get real coordinates of frontPoint to Scene coordinates
    CGPoint targetPointToSceneCoordinates = [[target parent] convertToWorldSpace: target.position];
    //NSLog(@"scene coord X: %f,   Y: %f",frontPointToSceneCoordinates.x,frontPointToSceneCoordinates.y);
    CGPoint diff = ccpSub(targetPointToSceneCoordinates, missile.position);
    float angle = atan2f(diff.y, diff.x);
    missile.rotation = 90.0f - CC_RADIANS_TO_DEGREES(angle);
    
    CCActionMoveTo *strike = [CCActionMoveTo actionWithDuration:1 position:targetPointToSceneCoordinates];
    CCActionRemove *actionRemove = [CCActionRemove action];
    [missile runAction:[CCActionSequence actionWithArray:@[strike,actionRemove]]];
    [[OALSimpleAudio sharedInstance] playEffect:@"Shoot2.wav"];
}


#pragma mark EnemyShip Actions

-(void)enemyShipMoveToPlayer
{
    if ([_physicsWorld.children containsObject:_enemyShip] && [_physicsWorld.children containsObject:_player.playerImage]) {
        CGPoint diff = ccpSub(_enemyShip.position, _player.playerImage.position);
        float distance = ccpLength(diff);
        //NSLog(@"%f",distance);
        if (distance <= ENEMY_SHIP_VISION_RADIUS ) {
            [self rotateEnemyShip];
            if (distance > ENEMY_SHIP_DISTANCE_FROM_PLAYER && [_enemyShip numberOfRunningActions] == 0) {
                NSLog(@"Move to PlayerShip!");
                CGPoint targetPosition = _player.playerImage.position;
                CCActionMoveTo *move = [CCActionMoveTo actionWithDuration:5 position:targetPosition];
                [_enemyShip runAction:move];
            } else if (distance <= ENEMY_SHIP_DISTANCE_FROM_PLAYER &&  [_enemyShip numberOfRunningActions] > 0) {
                NSLog(@"Stop Action on EnemyShip !");
                [_enemyShip stopAllActions];
            }
        }
    }
}

- (void)rotateEnemyShip
{
    CGPoint targetPosition = _player.playerImage.position;
    CGPoint diff = ccpSub(targetPosition, _enemyShip.position);
    float angle = atan2f(diff.y, diff.x);
	_enemyShip.rotation = 90.0f - CC_RADIANS_TO_DEGREES(angle);
}

-(void)enemyShipStrike
{
    // Check for EnemyShip and PlayerShip on Map
    if ([_physicsWorld.children containsObject:_enemyShip] && [_physicsWorld.children containsObject:_player.playerImage]) {
        CGPoint diff = ccpSub(_enemyShip.position, _player.playerImage.position);
        float distance = ccpLength(diff);
        
        // Check for TurretVision of the Ship
        if (distance <= ENEMY_SHIP_STRIKE_RADIUS)
        {
            [self rotateEnemyShip];
            if (CACurrentMediaTime() - _enemyShipStrikeTime > ENEMY_SHIP_STRIKE_INTERVAL ) {
                _enemyShipStrikeTime = CACurrentMediaTime();
                [self fireWithCannon:_enemyShip toSprite:_player.playerImage withCollisionType:@"enemyMissile" andCollisionGroup:@"enemyGroup"];
            }
        }
    }
}

#pragma mark EnemyTurret Actions

-(void)turretStrike
{
    // Check for Player and Turret on Map
    if ([_physicsWorld.children containsObject:_turretSprite] && [_physicsWorld.children containsObject:_player.playerImage]) {
        
        // Check for TurretVision of the PlayerShip
        CGPoint diff = ccpSub(_player.playerImage.position, _turretSprite.position);
        float distance = ccpLength(diff);
        
        //NSLog(@"distance: %f",distance);
        if (distance <= TURRET_VISION_RADIUS)
        {
            [self rotateTurret];
            if (CACurrentMediaTime() - _turretStrikeTime > TURRET_STRIKE_INTERVAL) {
                _turretStrikeTime = CACurrentMediaTime();
                [self fireWithCannon:_turretSprite toSprite:_player.playerImage withCollisionType:@"enemyMissile" andCollisionGroup:@"enemyGroup"];
            }
        }
    }
}

- (void)rotateTurret
{
    CGPoint targetPosition = _player.playerImage.position;
    CGPoint diff = ccpSub(targetPosition, _turretSprite.position);
    float angle = atan2f(diff.y, diff.x);
	_turretSprite.rotation = 90.0f - CC_RADIANS_TO_DEGREES(angle);
}

#pragma mark Collision Detection (PhysicsNode)

// EnemyShip with PlayerMissile
-(BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair enemyShip:(CCNode *)enemyShip playerMissile:(CCNode *)playerMissile
{
    [[OALSimpleAudio sharedInstance] playEffect:@"Hit3.wav"];
    _enemyShipHP -= 10;
    NSLog(@"EnemyShip HP: %d",_enemyShipHP);
    if (_enemyShipHP <= 0) {
        [_physicsWorld removeChild:enemyShip cleanup:YES];
        [self removeChild:_enemyShipHealthBar cleanup:YES];
    }
    [playerMissile removeFromParent];
    return NO;
}

//Coollision between PlayerMissile and Turret
-(BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair turretCollision:(CCNode *)turret playerMissile:(CCNode *)missile
{
    [[OALSimpleAudio sharedInstance] playEffect:@"Hit3.wav"];
    _turretHP -= 20;
    NSLog(@"Turret HP: %d",_turretHP);
    if (_turretHP <= 0) {
        [_physicsWorld removeChild:_turretSprite cleanup:YES];
        [self removeChild:_turretHealthBar cleanup:YES];
        return YES;
    }
    [missile removeFromParent];
    return NO;
}

//Player Ship with Enemy Missile
-(BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair shipCollision:(CCNode *)ship enemyMissile:(CCNode *)missile
{
    [[OALSimpleAudio sharedInstance] playEffect:@"Hit3.wav"];
    AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
    //[self shakePlayer];
    _playerHP -= 10;
    NSLog(@"Ship HP: %d",_playerHP);
    if (_playerHP <= 0) {
        [_physicsWorld removeChild:ship cleanup:YES];
        [self removeChild:_playerHealthBar cleanup:YES];
        return YES;
    }
    [missile removeFromParent];
    return NO;
}

//Player Ship with Enemy Ship
-(BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair shipCollision:(CCNode *)playerShip enemyShip:(CCNode *)enemyShip
{
    [[OALSimpleAudio sharedInstance] playEffect:@"Hit3.wav"];
    AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
    //[self shakePlayer];
    _playerHP -= 10;
    _enemyShipHP -= 10;
    NSLog(@"Ship HP: %d", _playerHP);
    if (_playerHP <= 0) {
        [_physicsWorld removeChild:playerShip cleanup:YES];
        [self removeChild:_playerHealthBar cleanup:YES];
        return YES;
    }
    if (_enemyShipHP <= 0) {
        [_physicsWorld removeChild:enemyShip cleanup:YES];
        [self removeChild:_enemyShipHealthBar cleanup:YES];
        return YES;
    }
    return NO;
}
#pragma mark Shake Player

-(void)shakePlayer
{
    //    1) AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
    //    2) AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
    CCAction *action = [self.player.playerImage getActionByTag:SHAKE_ACTION_TAG];
    if (action) {
        [self.player.playerImage stopAction:action];
        self.player.playerImage.rotation = _currentOrientationOfPlayer;
        //NSLog(@"Stop sequence action");
    }
    _currentOrientationOfPlayer = self.player.playerImage.rotation;
    CCActionRotateBy *rotateLeft = [[CCActionRotateBy alloc] initWithDuration:0.1f angle:3];
    CCActionRotateBy *rotateRight = [[CCActionRotateBy alloc] initWithDuration:0.1f angle:-6];
    CCActionRotateTo *rotateToCenter = [[CCActionRotateTo alloc]initWithDuration:0.0f angle:_currentOrientationOfPlayer];
    CCActionSequence *sequence = [CCActionSequence actions:rotateLeft,rotateRight,rotateToCenter, nil];
    sequence.tag = SHAKE_ACTION_TAG;
    [self.player.playerImage runAction:sequence];
    //NSLog(@"Run repeat action");
}


-(void) panAction: (UIPanGestureRecognizer*) pan{
    static CGPoint startLocation;
    UIView *piece = pan.view;
    if (pan.state == UIGestureRecognizerStateBegan) {
        startLocation = [pan locationInView:piece];
    }
    else if (pan.state == UIGestureRecognizerStateChanged) {
        CGPoint stopLocation = [pan locationInView:piece];
        CGFloat distance = (stopLocation.y - startLocation.y);
        NSLog(@"Distance: %f", distance);
        [self steerPlayer:distance];
//        [self rotateWithAngle:distance];
    }
}
//-(void)rotateWithAngle: (CGFloat) angle{
//    if(angle>0){
//            [_sprite runAction:[CCActionRotateBy actionWithDuration:angle/90 angle:angle*-1]];
//    }
//    else{
//            [_sprite runAction:[CCActionRotateBy actionWithDuration:(angle/90)*-1 angle:angle*-1]];
//        }
//}


@end
